﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;

namespace smx
{
    public partial class productEdit : Form
    {
        private long id = 0;
        private int compid = 0;

        //add form constructor
        public productEdit()
        {
            InitializeComponent();
        }

        //edit form constructor
        public productEdit(string prodid)
        {
            InitializeComponent();
        
            id =Convert.ToInt64(prodid);
        }

       
        //---------------------------------------------------------------------------------------------------------------------
        //load method for the form
        //---------------------------------------------------------------------------------------------------------------------
        private void productEdit_Load(object sender, EventArgs e)
        {
            if (id != 0)
            {
                stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                try
                {
                    var q = from p in data.products
                            where p.id == id
                            select p;
                    foreach (product p in q)
                    {
                        idTxt.Text = p.id.ToString();
                        NameTxt.Text = p.name;
                        valueTxt.Text = p.value.ToString();
                        notesTxt.Text = p.notes;
                        fullnameTxt.Text = p.name;
                        getAllComps();
                    }

                }
                catch (Exception err)
                {

                    cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //close the form if the user confirms the decision
        //---------------------------------------------------------------------------------------------------------------------
        private void cancelBtn_Click(object sender, EventArgs e)
        {
            if (
                cTaskDialog.MessageBox(this,
                    "Confirmation",
                    "Are you sure you wish to cancel?",
                    "Choosing yes will close this form and no data will be saved. Do you wish to proceed?",
                    eTaskDialogButtons.YesNo,
                    eSysIcons.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //fill the compslist box with all the components that this product has
        //---------------------------------------------------------------------------------------------------------------------
        public void getAllComps()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            try
            {
                var q = from c in data.compsforprods
                        where c.prodid == id
                        select c;
                compslist.DisplayMember = "name";
                compslist.ValueMember = "id";
                compslist.DataSource = q;



            }
            catch (Exception err)
            {

                cTaskDialog.MessageBox(this, "Error in Code",
                   "There was an error!",
                   "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                   err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //gets the details of the selected component
        //---------------------------------------------------------------------------------------------------------------------
        private void compslist_SelectedIndexChanged(object sender, EventArgs e)
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            long compid = Convert.ToInt64(compslist.SelectedValue);
            try
            {

                var q = (from c in data.components
                        where c.id == compid
                        select c.value).First();
                var all = (from a in data.allocs
                           where a.component_id == compid && 
                           a.product_id == id
                           select a.qtyneeded).First();

                QtyNeededTxt.Text = all.ToString();
                comppriceTxt.Text = q.ToString();
            }
            catch (Exception err)
            {  
                cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to get the records for the components, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //save the record, refresh main grid and close form, the saving is delegated depending on whether its
        //and edit form or an add form
        //---------------------------------------------------------------------------------------------------------------------
        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (id == 0)
            {
                addProd();
            }
            else
            {
                editProd();
            }

            MasterForm master = (MasterForm)Application.OpenForms["MasterForm"];
            master.getProducts();
            this.Close();

        }

        //---------------------------------------------------------------------------------------------------------------------
        //add a new product to the database
        //---------------------------------------------------------------------------------------------------------------------
        private void addProd()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            try
            {
                keyGen gen = new keyGen();
                long newid = gen.generateId();
                decimal pricevalue = Convert.ToDecimal(valueTxt.Text);
                product p = new product();

                p.id = newid;
                p.name = NameTxt.Text;
                p.value = pricevalue;
                p.notes = notesTxt.Text;

                data.AddToproducts(p);
                data.SaveChanges();
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                      "There was an error!",
                      "There was an error in attempting to add the  records for the new product, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                      err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
            

        }

        //---------------------------------------------------------------------------------------------------------------------
        //save the current details in the text boxes.
        //---------------------------------------------------------------------------------------------------------------------
        private void editProd()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            try
            {
                decimal pricevalue = Convert.ToDecimal(valueTxt.Text);

                var p = (from c in data.products
                         where c.id == id
                         select c).First();
                p.name = NameTxt.Text;
                p.value = pricevalue;
                p.notes = notesTxt.Text;

                data.SaveChanges();
                
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                                    "There was an error!",
                                    "There was an error in attempting to edit the  records for this product, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                                    err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);

            }
        }


        //---------------------------------------------------------------------------------------------------------------------
        //opens the component assignment dialog
        //---------------------------------------------------------------------------------------------------------------------
        private void compassBtn_Click(object sender, EventArgs e)
        {
            compAssignForm form = new compAssignForm();
            form.Show();
        }

        //---------------------------------------------------------------------------------------------------------------------
        //saves the changes to the currently selected allocation
        //---------------------------------------------------------------------------------------------------------------------
        private void saveAllocBtn_Click(object sender, EventArgs e)
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            try
            {
                long cid = (long)compslist.SelectedValue;

                var q = (from a in data.allocs
                         where a.component_id == cid
                         && a.product_id == id
                         select a).FirstOrDefault();

                q.qtyneeded = Convert.ToInt32(QtyNeededTxt.Text);

                data.SaveChanges();
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                              "There was an error!",
                              "There was an error in attempting to edit the  records for this product, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                              err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }
    }
}
